﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class VA_Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("FD1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                    return;
                }
                try
                {
                    lstCurrencies.SelectedValue = Session["DEFAULT_CURRENCY"].ToString();
                }
                catch (Exception ex)
                {
                    //Does nothing!
                }
            }
            else
            {
                lblError.Text = "";
                lblErrNew.Text = "";
            }

            if (lstStatus.Text == "E")
            {
                //panNew.Visible = true;
                //panAircraft.Visible = false;
                btnSubmit.Visible = true;
                btnReject.Visible = false;
                btnApprove.Visible = false;
                btnClose.Visible = false;
                btnDelete.Visible = true;
            }
            else if (lstStatus.Text == "S")
            {
                //panNew.Visible = false;
                //panAircraft.Visible = false;
                btnSubmit.Visible = false;
                btnReject.Visible = true;
                btnApprove.Visible = true;
                btnClose.Visible = false;
                btnDelete.Visible = false;
            }
            else if (lstStatus.Text == "A")
            {
                //panNew.Visible = false;
                //panAircraft.Visible = false;//简化,emd不控制飞机载量能力
                btnSubmit.Visible = false;
                btnReject.Visible = false;
                btnApprove.Visible = false;
                btnClose.Visible = true;
                btnDelete.Visible = false;
            }
            else//C-Closed
            {
                //panNew.Visible = false;
                //panAircraft.Visible = false;
                btnSubmit.Visible = false;
                btnReject.Visible = false;
                btnApprove.Visible = false;
                btnClose.Visible = false;
                btnDelete.Visible = false; //关闭的通过系统自动归档删除!
            }
            btnSubmit.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnReject.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnApprove.Attributes.Add("onclick", "javascript:return confirm('You can not modify fare information after it was approved! Are you sure to approve it?');");
            btnClose.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            //ScriptManager1.RegisterPostBackControl(btnAdd);
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrNew.Text = "Unauthorized!";
                return;
            }

            txtID.Text = txtID.Text.ToUpper();
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Z0-9]+$");
            if (!reg1.IsMatch(txtID.Text))
            {
                lblErrNew.Text = "Only english characters or number characters can show in ID!";
                return;
            }
            int ret_code;

            try
            {
                SqlCommand cmd = new SqlCommand("bll_emd_new");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@id", txtID.Text);
                para = cmd.Parameters.AddWithValue("@type", lstTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@currency", lstCurrencies.Text);
                para = cmd.Parameters.AddWithValue("@price", Convert.ToSingle(txtPrice.Text));
                //para = cmd.Parameters.AddWithValue("@service_place", lstServicePlace.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@flt_date_from", Convert.ToDateTime(txtFltDateFrom.Text));
                //para = cmd.Parameters.AddWithValue("@flt_date_to", Convert.ToDateTime(txtFltDateTo.Text));
                para = cmd.Parameters.AddWithValue("@close_before_flt", txtCloseBefore.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrNew.Text = "Done.";
                    GridView1.DataBind();
                }
                else
                    lblErrNew.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrNew.Text = ex.Message;
                else
                    lblErrNew.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";

            cmd.CommandText = "bll_emd_delete";
            SqlParameter para = cmd.Parameters.AddWithValue("@emd_id", strArray[1]);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Deleting failed! Inproper status.";
                else if (ret_code == 2)
                    lblError.Text = "Deleting failed! Maybe the record is not exist.";
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnApprove_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            int ret_code;
            SqlCommand cmd = new SqlCommand("bll_emd_approve");
            SqlParameter para = cmd.Parameters.AddWithValue("@id", strArray[1]);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                {
                    lblError.Text = "Approving failed!";
                }
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnReject_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            int ret_code;
            SqlCommand cmd = new SqlCommand("bll_emd_reject");
            SqlParameter para = cmd.Parameters.AddWithValue("@id", strArray[1]);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            int ret_code;
            SqlCommand cmd = new SqlCommand("bll_emd_close");
            SqlParameter para = cmd.Parameters.AddWithValue("@id", strArray[1]);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                {
                    lblError.Text = "Failed!";
                }
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_value;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand("bll_emd_submit");
            SqlParameter para = cmd.Parameters.AddWithValue("@id", strArray[1]);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_value = TinyDAL.Exec(ref cmd);
                if (ret_value == 0)
                {
                    lblError.Text = "Submitted successfully!";
                    GridView1.DataBind();
                }
                else if (ret_value == 2)
                {
                    lblError.Text = "Submitting failed!";
                    GridView1.DataBind();
                }
                else
                {
                    lblError.Text = "Error " + ret_value.ToString() + "! Fare is not OK! Please check segments,fare-stairs and rules!";
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnLog_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("VAP_Log.aspx?id=" + strArray[1]);
        }
        protected void btnDetails_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("VAP_Details.aspx?emd_id=" + strArray[1]);
        }
        //protected void lstAcTypes_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    lstAircraft.DataBind();
        //}
        //protected void btnAddEmds_Click(object sender, EventArgs e)
        //{
        //    if (Session["ACL"].ToString().IndexOf("AG2") < 0)
        //    {
        //        lblErrAC.Text = "Unauthorized!";
        //        return;
        //    }
        //    int ret_code;
        //    if (!int.TryParse(txtMax.Text, out ret_code))
        //    {
        //        lblErrAC.Text = "Max Load should be a number!";
        //        return;
        //    }

        //    foreach (ListItem item in lstEmds.Items)
        //    {
        //        if (item.Selected)
        //        {
        //            try
        //            {
        //                SqlCommand cmd = new SqlCommand("usp_add_ac_emd");
        //                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
        //                para = cmd.Parameters.AddWithValue("@tail_no", lstAircraft.SelectedValue);
        //                para = cmd.Parameters.AddWithValue("@emd_id", item.Value);
        //                para = cmd.Parameters.AddWithValue("@max_load", txtMax.Text);
        //                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
        //                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
        //                //para = new SqlParameter("@ret_code", SqlDbType.Int);
        //                //para.Direction = ParameterDirection.Output;
        //                //cmd.Parameters.Add(para);
        //                ret_code = TinyDAL.Exec(ref cmd);
        //                if (ret_code == 0)
        //                {
        //                    lblErrAC.Text = "Done.";
        //                }
        //                else if (ret_code == 1)
        //                {
        //                    lblErrAC.Text = "Adding failed! The aircraft is not belong to your airline.";
        //                    break;
        //                }
        //                else if (ret_code == 2)
        //                {
        //                    lblErrAC.Text = "Adding failed! The EMD is not belong to your airline.";
        //                    break;
        //                }
        //                else if (ret_code == 3)
        //                {
        //                    lblErrAC.Text = "Adding failed: " + item.Value;
        //                    break;
        //                }
        //                else
        //                    lblErrAC.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
        //            }
        //            catch (Exception ex)
        //            {
        //                if (Application["DEBUG"].ToString() == "1")
        //                    lblErrAC.Text = ex.Message;
        //                else
        //                    lblErrAC.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
        //            }
        //        }
        //    }
        //    viewAcEMDs.DataBind();
        //}
        //protected void cmdRefresh_Click(object sender, EventArgs e)
        //{
        //    viewAcEMDs.DataBind();
        //}
        //protected void btnDeleteEmd_Click(object sender, EventArgs e)
        //{
        //    if (Session["ACL"].ToString().IndexOf("AG2") < 0)
        //    {
        //        lblErrAC.Text = "Unauthorized!";
        //        return;
        //    }
        //    string key;
        //    string[] strArray;
        //    if (Request.Form["KEY"] == null)
        //    {
        //        lblError.Text = "Please select the ticket.";
        //        return;
        //    }
        //    key = Request.Form["KEY"];
        //    strArray = key.Split('|');
        //    try
        //    {
        //        int ret_code;
        //        SqlCommand cmd = new SqlCommand("usp_delete_ac_emd");
        //        SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
        //        para = cmd.Parameters.AddWithValue("@tail_no", strArray[1]);
        //        para = cmd.Parameters.AddWithValue("@emd_id", strArray[0]);
        //        para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
        //        para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
        //        //para = new SqlParameter("@ret_code", SqlDbType.Int);
        //        //para.Direction = ParameterDirection.Output;
        //        //cmd.Parameters.Add(para);
        //        ret_code = TinyDAL.Exec(ref cmd);
        //        if (ret_code == 0)
        //        {
        //            lblErrAC.Text = "Done.";
        //            viewAcEMDs.DataBind();
        //        }
        //        else if (ret_code == 1)
        //            lblErrAC.Text = "Deleting failed! EMD not found, or EMD is not editing.";
        //        else if (ret_code == 2)
        //            lblErrAC.Text = "Deleting failed! Maybe the record is not exist.";
        //        else
        //            lblErrAC.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
        //    }
        //    catch (Exception ex)
        //    {
        //        if (Application["DEBUG"].ToString() == "1")
        //            lblErrAC.Text = ex.Message;
        //        else
        //            lblErrAC.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
        //    }
        //}
        protected void lstTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstTypes.SelectedValue == "S")
            {
                Label16.Visible = false;
                Label17.Visible = false;
                txtCloseBefore.Visible = false;
            }
            else
            {
                Label16.Visible = true;
                Label17.Visible = true;
                txtCloseBefore.Visible = true;
            }
        }
    }
}